#include "MyGame.h"

#include "userStateList.h"

#define TOTAL_LIVES 5

MyGame* MyGame::m_MyGame = NULL;

MyGame::MyGame()
{
    Penjin::setApplicationName("The Pandora Game");
    nextLevel = NULL;
}

MyGame::~MyGame()
{

}

MyGame *MyGame::GetMyGame()
{
    if (not m_MyGame)
        m_MyGame = new MyGame;
    return m_MyGame;
}

PENJIN_ERRORS MyGame::init()
{
    GFX::setResolution(800,480);
    GFX::resetScreen();

    resetVariables();

    #ifdef _DEBUG
    setInitialState(STATE_MAIN);
    #else
    setInitialState(STATE_TITLE);
    #endif

    return PENJIN_OK;
}

void MyGame::stateManagement()
{
    if (nextLevel) // A next level has been specified, let's switch to it (this is the case after the intro)
    {
        delete state;
        state = nextLevel;
        nextLevel = NULL;
        return;
    }
    else
    {
        uint next = state->getNextState();
        BaseState* temp = state;    // Preserve current level for scoring

        if(state)
            state = NULL;

        switch(next) // normal state check
        {
        case STATE_TITLE: // Title: logos, intro
        {
            state = new StateTitle;
            break;
        }
        case STATE_MAIN: // Main menu
        {
            state = new StateMain;
            resetVariables();
            break;
        }
        case STATE_LEVELSELECT:
        {
            state = new StateLevelSelect;
            DataState* level = getStateInformation(STATE_DUMMY + 1);
            for (int I = STATE_DUMMY + 2; level; ++I)
            {
                ((StateLevelSelect*)state)->addLevel(level);
                level = getStateInformation(I);
            }
            ++singlePlay;
            break;
        }
        case STATE_SCORE: // Score screen after level
        {
            state = new StateScore;
            lives = ((GameBaseState*)temp)->lives;
            ((StateScore*)state)->singleScore = ((GameBaseState*)temp)->score;
            ((StateScore*)state)->totalScore = globalScore;
            ((StateScore*)state)->nextLevel = ((GameBaseState*)temp)->stateID + 1;
            break;
        }
        case STATE_GAMEOVER: // Gmae over screen after all levels
        {
            state = new StateGameover;
            ((StateGameover*)state)->score = globalScore;
            break;
        }
        default: // Games
        {
            if (singlePlay == 2)
            {
                state = new StateGameover;
                ((StateGameover*)state)->score = globalScore;
                delete temp;
                return;
            }
            else if (singlePlay == 1)
                ++singlePlay;

            nextLevel = createUserState(next);

            if (not nextLevel)
            {
                state = new StateGameover;
                ((StateGameover*)state)->score = globalScore;
                delete temp;
                return;
            }

            ((GameBaseState*)nextLevel)->lives = lives;

            state = new StateIntroduction;
            DataState* stateData = getStateInformation(next);
            ((StateIntroduction*)state)->levelData = stateData;
            ((StateIntroduction*)state)->lives = lives;
        } // default
        } // switch

        delete temp;
    }
}

PENJIN_ERRORS MyGame::argHandler(int argc, char **argv)
{
    for (int I = 0; I < argc; ++I)
    {
        cout << argv[I] << " ";
    }
    cout << endl;

    if(argc <= 1)
        return	PENJIN_NO_COMMANDLINE;
    else
    {
        //	Do further CMD processing
        for (int arg = 1; arg < argc; ++arg)
        {
            //	Check for commandline escape chars
            if(argv[arg][0] == '-' || argv[arg][0] == '/')
            {
                //	Look for arguments
                switch(argv[arg][1])
                {
                    //	Set Fullscreen
                case 'F':
                {
                    GFX::setFullscreen(true);
                    break;
                }
#ifdef PLATFORM_GP2X
                case 'M':
                case 'm':
                {
                    loadMenu = true;
                    break;
                }
#endif
                default:
                {
                    return PENJIN_INVALID_COMMANDLINE;
                }
                }
            }
        }
    }
    return	PENJIN_OK;
}

void MyGame::resetVariables()
{
    globalScore = 0;
    lives = TOTAL_LIVES;
    delete nextLevel;
    nextLevel = NULL;
    singlePlay = 0;
}
